package com.example.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shopapp.R;
import com.example.viewmodel.Groupon;
import com.example.viewmodel.LeftMenu;

import java.util.List;

public class CurrentSubCategoryAdatpter extends RecyclerView.Adapter<CurrentSubCategoryAdatpter.ViewHolder> {
    public Context ctx;
    public List<LeftMenu> currentSubCategory;

    public CurrentSubCategoryAdatpter(Context ctx, List<LeftMenu> currentSubCategory) {
        this.ctx = ctx;
        this.currentSubCategory = currentSubCategory;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.ctx).inflate(R.layout.fragment_catalog_content_item, parent, false);
        CurrentSubCategoryAdatpter.ViewHolder viewHolder = new CurrentSubCategoryAdatpter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.fragment_catalog_content_item_txt.setText(this.currentSubCategory.get(position).getName()+"");

        // 渲染图片（假设 groupon 对象有 getImageUrl() 方法）
        String imageUrl = this.currentSubCategory.get(position).getPicUrl();
        Glide.with(holder.itemView.getContext())
                .load(imageUrl) // 图片 URL
                .into(holder.fragment_catalog_content_item_img); // 目标 ImageView
    }

    @Override
    public int getItemCount() {
        return this.currentSubCategory.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView fragment_catalog_content_item_img;
        TextView fragment_catalog_content_item_txt;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.fragment_catalog_content_item_img = itemView.findViewById(R.id.fragment_catalog_content_item_img);
            this.fragment_catalog_content_item_txt = itemView.findViewById(R.id.fragment_catalog_content_item_txt);
        }
    }
}
